<template>
    <div class="app-container">
        <!-- 查询条件开始 -->
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="98px">
            <el-form-item label="患者姓名" prop="patientName">
                <el-input
                        v-model="queryParams.patientName"
                        placeholder="请输入患者姓名"
                        clearable
                        size="small"
                        style="width:240px"
                />
            </el-form-item>
            <el-form-item label="挂号单号" prop="regId">
                <el-input
                        v-model="queryParams.regId"
                        placeholder="请输入挂号单号"
                        clearable
                        size="small"
                        style="width:240px"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetSearchForm">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 查询结束  -->

        <!-- 数据表格开始 -->
        <el-table border :data="orderChargeTableList">
            <el-table-column label="订单号" align="center" width="210" prop="orderId" />
            <el-table-column label="挂号单号" align="center" width="200" prop="regId" />
            <el-table-column label="患者姓名" align="center" prop="patientName" />
            <el-table-column label="总金额" align="center" prop="orderAmount" />
            <el-table-column label="支付类型" align="center" prop="payType" :formatter="typeFormatter"/>
            <el-table-column label="订单状态" prop="orderStatus" align="center"  :formatter="statusFormatter"/>
            <el-table-column label="创建时间" align="center" prop="createTime" />
            <el-table-column label="操作" width="300px" align="center">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-view" size="mini" @click="handleViewItem(scope.row)">查看详情</el-button>
                    <el-button type="text" :disabled="scope.row.orderStatus==='1'" icon="el-icon-bank-card" size="mini" @click="handlePayWithCash(scope.row)">现金收费</el-button>
                    <el-button type="text" :disabled="scope.row.orderStatus==='1'" icon="el-icon-bank-card" size="mini" @click="handlePayWithZfb(scope.row)">支付宝收费</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 数据表格结束 -->

        <!-- 查看详情的弹出层开始 -->
        <el-dialog
                :title="title"
                :visible.sync="open"
                width="1000px"
                center
                append-to-body

        >
            <el-table border :data="orderChargeItemList" >
                <el-table-column label="详情ID" align="center" width="210" prop="itemId" />
                <el-table-column label="处方ID" align="center" width="200" prop="coId" />
                <el-table-column label="名称" align="center" prop="itemName" />
                <el-table-column label="价格" align="center" prop="itemPrice" />
                <el-table-column label="数量" align="center" prop="itemNum" />
                <el-table-column label="小计" prop="itemAmount" align="center" />
                <el-table-column label="类型" align="center" prop="itemType" >
                    <template slot-scope="scope">
                        {{ scope.row.itemType==='0'?'药品':'检查' }}
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center" prop="status"  :formatter="itemStatusFormat"/>
            </el-table>
            <span slot="footer" class="dialog-footer">
                <el-button @click="open=false">关 闭</el-button>
            </span>
        </el-dialog>
        <!-- 查看详情的弹出层结束 -->

        <!-- 分页控件开始 -->
        <el-pagination
                v-show="total>0"
                :current-page="currentPage"
                :page-sizes="[5, 10, 20, 30]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        />
        <!-- 分页控件结束 -->
        <!-- 支付宝支付的弹出层开始 -->
        <el-dialog
                title="请使用支付宝支付"
                :visible.sync="openPay"
                center
                :close-on-click-modal="false"
                :before-close="handleClose"
                append-to-body>
            <el-form label-position="left" label-width="120px" >
                <el-card>
                    <el-form-item label="订单号:">
                        <span class="txt">{{ payObj.orderId }}</span>
                    </el-form-item>
                    <el-form-item label="总金额:">
                        <span>{{ payObj.price }}</span>
                    </el-form-item>
                </el-card>
            </el-form>
            <div style="text-align:center" class="txt">
                <vue-qr :text="payObj.codeUrl" :size="200" />
                <div>请使用支付宝扫码</div>
            </div>
        </el-dialog>
        <!-- 支付宝支付的弹出层结束 -->
    </div>
</template>

<script>
    import Vue from "vue";
    import vueQr from "vue-qr";
    export default {

        components:{
            vueQr
        },
        name: "Chargelist",
        data(){
            return {
                //数据表格内容
                orderChargeTableList: [],
                // 查询参数
                queryParams: {
                    patientName: undefined,
                    regId: undefined
                },
                // 分页
                total: 0,
                pageSize: 5,
                currentPage:1,

                //数据字典 支付状态
                typeOptions:[],
                //数据字典 类型
                statusOptions:[],

                //查看详情是否出现
                open: false,
                // 弹出层标题
                title: '',

                //详情里面的数据
                orderChargeItemList:[],
                //详情中状态
                itemStatusOptions:[],

                // 支付对象
                payObj: {},
                // 是否打开支付宝二维码支付层
                openPay:false,

            }
        },
        methods:{
            //查询所有数据
            initQuery(){
                this.$axios.post("/charge/api/orderCharge/queryCharge/"+this.currentPage+"/"+this.pageSize,this.queryParams).then(result=>{
                    this.orderChargeTableList=result.data.t.records;
                    this.total=result.data.t.total;
                })
            },
            //查询表格中数据
            handleQuery(){
                this.currentPage=1
                this.initQuery();
            },
            //查询支付类型
            initType(){
                this.$axios.get("/charge/api/orderCharge/getByType/his_pay_type_status").then(r=>{
                    this.typeOptions=r.data.t;
                })
            },
            //对类型格式化解析
            typeFormatter(row){
                let v=row.payType;
                return this.formatDict(this.typeOptions,v);
            },
            //查询状态
            initStatus(){
                this.$axios.get("/charge/api/orderCharge/getByType/his_order_charge_status").then(r=>{
                    this.statusOptions=r.data.t;
                })
            },
            //对状态解析
            statusFormatter(row){
                let v=row.orderStatus;
                return this.formatDict(this.statusOptions,v);
            },


            //查看详情
            handleViewItem(row){
                this.open=true;
                this.title = '查询' + row.patientName + '支付订单详情';
                this.$axios.get("charge/api/orderChargeItem/allChargeItem/"+row.orderId).then(r=>{
                    this.orderChargeItemList= r.data.t;
                })
            },
            //查询详情中的状态
            initItemStatus(){
                this.$axios.get("/charge/api/orderCharge/getByType/his_order_details_status").then(r=>{
                    this.itemStatusOptions=r.data.t;
                })
            },
            itemStatusFormat(row){
                return this.formatDict(this.itemStatusOptions, row.status)
            },

            // 现金收费
            handlePayWithCash(row) {
                this.$confirm('是否确定现金支付?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {

                    this.$axios.get("/charge/api/orderCharge/payOrder/"+row.orderId).then(r => {
                        this.$message({
                            type: 'success',
                            message: '现金支付成功!'
                        });
                        this.initQuery()// 刷新列表
                    }).catch(() => {
                        Vue.prototype.$message.error("现金支付失败")
                    })
                }).catch(() => {
                    Vue.prototype.$message.error("现金支付已取消")
                })
            },
            // 支付宝收费
            handlePayWithZfb(row) {
               this.$axios.post("/charge/api/orderCharge/payOrderZfb/"+row.orderId).then(res => {

                   const tx = this
                   this.openPay = true// 打开支付的弹出层
                   this.payObj = res.data.t

                    // 定时轮询
                    tx.intervalObj = setInterval(function() {
                        // 根据ID查询订单信息
                        tx.$axios.post("/charge/api/orderCharge/queryPayStatus/"+tx.payObj.orderId).then(res => {
                            if (res.data.t === true) { // 说明订单状态为支付成功
                                // 清空定时器
                                clearInterval(tx.intervalObj)
                                tx.$notify({
                                    title: '支付成功',
                                    message: '【' + tx.payObj.orderId + '】的订单编写支付成功',
                                    type: 'success'
                                })
                                tx.openPay = false;
                                tx.initQuery();

                            }
                        }).catch(() => {
                            this.$message.error(res.data.msg);
                            // 清空定时器
                            clearInterval(tx.intervalObj)
                        })
                    }, 200)
                }).catch(() => {
                    Vue.prototype.$message.error('操作失败')
                })
            },
            // 如果用户没有支付，而弹出层被关闭了
            handleClose() {
                this.$confirm('您确定放弃支付吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    Vue.prototype.$message.error('您已放弃支付，可以回到收费查询列表里面再次支付')
                    this.openPay = false
                    // 关闭轮询
                    clearInterval(this.intervalObj)
                }).catch(() => {
                    Vue.prototype.$message.error('欢迎继续支付')
                })
            },

            // 分页pageSize变化时触发
            handleSizeChange(val) {
                this.pageSize = val
                // 重新查询
                this.initQuery()
            },
            // 点击上一页  下一页，跳转到哪一页面时触发
            handleCurrentChange(val) {
                this.currentPage = val
                // 重新查询
                this.initQuery()
            },

            //重置
            resetSearchForm(){
                this.queryParams={};
                this.initQuery();
            },
            //关闭查看详情
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {});
            }
        },
        created() {
            //查询所有
            this.initQuery();

            //加载查询表格数据
            this.handleQuery();

            //支付类型
            this.initType();
            //订单状态
            this.initStatus();
            //详情中状态
            this.initItemStatus();

        }
    }
</script>

<style scoped>
    .demo-table-expand {
        font-size: 0;
    }
    .demo-table-expand label {
        width: 90px;
        color: #99a9bf;
    }
    .demo-table-expand .el-form-item {
        margin-right: 0;
        margin-bottom: 0;
        width: 50%;
    }
</style>